home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Color Picker SDK / Sample Code / ScrapPicker Sample / Sources ƒ / ScrapPickerUtils.c < prev   
Encoding:
Text File  |  1997-06-13  |  5.6 KB  |  231 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ScrapPickerUtils.c
  3.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  4. */
  5.  
  6.  
  7. //===============================================================================
  8. //
  9. //                                  ScrapPickerUtils.c
  10. //
  11. //===============================================================================
  12.  
  13.  
  14.  
  15. #include <TextUtils.h>            // <-- Public interfaces.
  16. #include "ScrapPicker.h"        // <-- Project interfaces.
  17. #include "PickerCommon.h"
  18.  
  19.  
  20. static void CheckMouseCursor (PickerStorageHandle);
  21.  
  22.  
  23. //=====================================================================  Functions
  24. //---------------------------------------------------------------------  ActivatePicker
  25.  
  26. void ActivatePicker (PickerStorageHandle storage)
  27. {    
  28.     (*storage)->active = true;
  29. }
  30.  
  31. //---------------------------------------------------------------------  DeactivatePicker
  32.  
  33. void DeactivatePicker (PickerStorageHandle storage)
  34. {    
  35.     (*storage)->active = false;
  36. }
  37.  
  38. //---------------------------------------------------------------------  DoMouseDown
  39.  
  40. OSErr DoMouseDown (PickerStorageHandle storage, EventData *data)
  41. {
  42. #pragma unused (storage, data)
  43.     
  44.     return noErr;
  45. }
  46.  
  47. //---------------------------------------------------------------------  DoKeyDown
  48. // Mainly this function is used to intercept the keystrokes and Beep if
  49. // an illegal character is entered.
  50.  
  51. OSErr DoKeyDown (PickerStorageHandle storage, EventData *data)
  52. {
  53. #pragma unused (storage, data)
  54.     
  55.     return noErr;
  56. }
  57.  
  58. //---------------------------------------------------------------------  DoKeyUp
  59.  
  60. OSErr DoKeyUp (PickerStorageHandle storage, EventData *data)
  61. {
  62. #pragma unused (storage, data)
  63.     
  64.     return noErr;
  65. }
  66.  
  67. //---------------------------------------------------------------------  DoIdle
  68.  
  69. OSErr DoIdle (PickerStorageHandle storage, EventData *data)
  70. {
  71. #pragma unused (data)
  72.     
  73.     CheckMouseCursor(storage);
  74.     
  75.     return noErr;
  76. }
  77.  
  78. //---------------------------------------------------------------------  DoListClick
  79.  
  80. OSErr DoListClick (PickerStorageHandle storage, ItemHitData *data)
  81. {
  82.     Rect                origColorRect;
  83.     Boolean                inBox;
  84.     Boolean                lastInBox;
  85.     PMColor                newTemp;
  86.     Point                where;
  87.     PickerStoragePtr    pStorage;
  88.     OSErr                theErr;
  89.     
  90.     theErr = noErr;
  91.     
  92.     GetItemRect(storage, iOrigColor, &origColorRect);
  93.     
  94.     inBox = true;
  95.     lastInBox = false;
  96.     
  97.     pStorage = *storage;
  98.     
  99.     newTemp = pStorage->currPMColor;
  100.     
  101.     do
  102.     {
  103.         GetMouse(&where);
  104.         inBox = PtInRect(where, &origColorRect);
  105.         if (inBox != lastInBox)
  106.         {
  107.             lastInBox = inBox;
  108.             if (inBox)
  109.                 pStorage->currPMColor = pStorage->origPMColor;
  110.             else
  111.                 pStorage->currPMColor = newTemp;
  112.             
  113.                 // Update the text.
  114.             DrawColorEditor(storage, kDrawNew + kDrawProc);
  115.         }
  116.     }
  117.     while (Button());
  118.     
  119.     if (inBox)
  120.     {
  121.         data->action = kColorChanged;
  122.         pStorage->wasColor = newTemp;
  123.     }
  124.     
  125.     return theErr;
  126. }
  127.  
  128. //---------------------------------------------------------------------  PtInItem
  129.  
  130. Boolean PtInItem (PickerStorageHandle storage, Point where, SInt16 itemNo)
  131. {
  132.     Rect        itemBounds;
  133.     
  134.     GetItemRect(storage, itemNo, &itemBounds);
  135.     
  136.     return (PtInRect(where, &itemBounds));
  137. }
  138.  
  139. //---------------------------------------------------------------------  GetItemRect
  140. // Using the port in 'storage', this function returns the bounds of the
  141. // item requested.
  142.  
  143. void GetItemRect (PickerStorageHandle storage, SInt16 itemNumber, Rect *itemBounds)
  144. {
  145.     Handle        itemHandle;
  146.     SInt16        itemType;
  147.     
  148.     GetDialogItem((DialogPtr)((*storage)->port), (*storage)->baseItem + itemNumber, 
  149.             &itemType, &itemHandle, itemBounds);
  150. }
  151.  
  152. //---------------------------------------------------------------------  GetItemHandle
  153. // Using the port in 'storage', this function returns a handle to the dialog
  154. // item requested.
  155.  
  156. Handle GetItemHandle (PickerStorageHandle storage, SInt16 itemNumber)
  157. {
  158.     Rect        itemBounds;
  159.     Handle        itemHandle;
  160.     SInt16        itemType;
  161.     
  162.     GetDialogItem((DialogPtr)((*storage)->port), (*storage)->baseItem + itemNumber, 
  163.             &itemType, &itemHandle, &itemBounds);
  164.     check(itemHandle != 0L);
  165.     
  166.     return itemHandle;
  167. }
  168.  
  169. //---------------------------------------------------------------------  SetColorPattern
  170. // Create a pattern to represent either the original or new color.
  171.  
  172. void SetColorPattern (PickerStorageHandle storage, ColorType whichColor)
  173. {
  174.     RGBColor            rgb;
  175.     PixPatHandle        pat;
  176.     PickerStoragePtr    pStorage;
  177.     
  178.     pStorage = *storage;
  179.     
  180.     if (whichColor == kOriginalColor)
  181.     {
  182.         pat = pStorage->origColorPat;
  183.         rgb = *(RGBColor *)&(pStorage->origPMColor.color).rgb;
  184.     }
  185.     else
  186.     {
  187.         pat = pStorage->newColorPat;
  188.         rgb = *(RGBColor *)&(pStorage->currPMColor.color).rgb;
  189.     }
  190.     
  191.     SetPatternToColor(pat, &rgb, pStorage->useColorPats);
  192. }
  193.  
  194. //---------------------------------------------------------------------  SelectDeselectItemText
  195.  
  196. void SelectDeselectItemText (PickerStorageHandle storage, SInt16 itemNo, Boolean selectIt)
  197. {
  198.     PickerStoragePtr    pStorage;
  199.     GrafPtr                thePort;
  200.     SInt16                baseItem;
  201.     
  202.     pStorage = *storage;
  203.     thePort = pStorage->port;
  204.     baseItem = pStorage->baseItem;
  205.     
  206.                 // If itemNo is zero we look at the editField in order to select the
  207.                 // current item.  Otherwise we select the item number passed in.
  208.     if (itemNo == 0)
  209.         itemNo = ((DialogPeek)thePort)->editField + 1 - baseItem;
  210.     
  211.     if ((itemNo > 0) && (itemNo <= iLastItem))
  212.     {
  213.         if (selectIt)
  214.             SelectDialogItemText(thePort, itemNo + baseItem, 0, 1024);
  215.         else
  216.             SelectDialogItemText(thePort, itemNo + baseItem, 0, 0);
  217.     }
  218. }
  219.  
  220. #pragma mark -------------------- Private Functions
  221. //=====================================================================  Private Functions
  222. //---------------------------------------------------------------------  CheckMouseCursor
  223.  
  224. static void CheckMouseCursor (PickerStorageHandle storage)
  225. {
  226.                 // If we're active (frontmost) and visible, update the cursor.
  227.     if ((*storage)->active && (*storage)->visible)
  228.         InitCursor();
  229. }
  230.  
  231.